package com.terrynoya.fight.stateMachine.controllers.attrParser
{
	import com.terrynoya.coreLib.MHashMap;
	import com.terrynoya.fight.combat.FightEngine;
	import com.terrynoya.fight.io.KeyValuePair;
	import com.terrynoya.fight.io.TextSection;
	import com.terrynoya.fight.stateMachine.StateController;

	public class StateCtrlAttrBuilder
	{
		private var _engine:FightEngine;
		
		private var _convertMap:MHashMap;
		
		public function StateCtrlAttrBuilder(engine:FightEngine)
		{
			this._engine = engine;	
			this._convertMap = new MHashMap();
			this._convertMap.put(StateCtrlParserType.Expression,new ExpressionConvert());
			this._convertMap.put(StateCtrlParserType.StringConvert,new StringConvert());
		}
		
		/**
		 * * [State 3000, 4]
			type = VelSet
			trigger1 = AnimElem = 5
			trigger2 = AnimElem = 13
			trigger3 = AnimElem = 21
			x = 6
 		中，解析除type，trigger(n),triggerAll之外的属性
		 * 如x=6		6可能是其他复杂表达式 
		 * @param ctrl
		 * @param section
		 * 
		 */		
		public function build(ctrl:StateController,section:TextSection):void
		{
			if(ctrl == null)
			{
				return;
			}
			var ctrlAttrMap:MHashMap = ctrl.attrConvertMap;
			for (var i:int = 0; i < section.parsedLines.length; i++) 
			{
				var vo:KeyValuePair = section.parsedLines[i];
				if(vo.key.indexOf("trigger") != -1 || vo.key == "type")
				{
					continue;
				}
				var parser:IStateCtrlAttrParser;
				if(ctrlAttrMap.containsKey(vo.key))
				{
					var type:int = ctrlAttrMap.getValue(vo.key);
					parser = this._convertMap.getValue(type);
				}
				else
				{
					parser = this._convertMap.getValue(StateCtrlParserType.Expression);
				}
				parser.parse(ctrl,vo,this._engine);
				ctrl.addAttrbute(vo.key,vo.value);
			}
			ctrl.build();
		}
	}
}